package com.zw.cc.Exception;

import com.zw.cc.base.BaseResult;
import com.zw.cc.base.ExceVariables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zwz
 * date 2016/12/26
 * project com.zw.cc.Exception
 */
@ControllerAdvice
public class BaseExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseExceptionHandler.class);
    /**
     * 针对于 @Validated de 验证错误返回数据
     * @param request
     * @param response
     * @param b
     * @return
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public BaseResult exp(HttpServletRequest request, HttpServletResponse response, BindException b) {
        return new BaseResult(ExceVariables.ACCOUNT_PWD_ERROR,b.getFieldError().getDefaultMessage());
    }
}
